package com.estagio.bean;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.primefaces.context.RequestContext;
import org.primefaces.event.TabChangeEvent;

import com.estagio.entity.Cargo;
import com.estagio.session.CargoSession;

@ManagedBean(name="cargoBean")
@SessionScoped
public class CargoBean {
	
	private Cargo cargo = new Cargo();
	private DataModel dmCargo;
	private String mensagem;

	public Cargo getCargo() {
		return cargo;
	}

	public void setCargo(Cargo cargo) {
		this.cargo = cargo;
	}


	public DataModel getDmCargo() {
		return dmCargo;
	}

	public void setDmCargo(DataModel dmCargo) {
		this.dmCargo = dmCargo;
	}

	public void saveEditCargo() {
		CargoSession cargoRN = new CargoSession();
		Cargo cargo = this.cargo;		
		this.mensagem = cargoRN.save(cargo);

		FacesMessage faces = new FacesMessage(mensagem);
		System.out.println(mensagem);
		FacesContext contexto = FacesContext.getCurrentInstance();
		contexto.addMessage(null, faces);		
		RequestContext.getCurrentInstance().update("formManterCargo:messagesCargo");
	}	

	public void saveCargo() {
		CargoSession cargoRN = new CargoSession();
		Cargo cargo = this.cargo;		
		this.mensagem = cargoRN.save(cargo);
		
		FacesMessage faces = new FacesMessage(this.mensagem);
		FacesContext contexto = FacesContext.getCurrentInstance();
		contexto.addMessage(null, faces);		
	}


	public String deleteCargo() {
		CargoSession cargoRN = new CargoSession();		
		Cargo cargo = (Cargo) this.dmCargo.getRowData();
		this.mensagem = cargoRN.delete(cargo);
		FacesMessage faces = new FacesMessage(mensagem);
		FacesContext contexto = FacesContext.getCurrentInstance();
		contexto.addMessage(null, faces);
		return "consultarCargo";
	}

	
	//FIND PROJETO / ATIVIDADE
	
	public DataModel findCargo() {
		List<Cargo> lista = (List<Cargo>) new CargoSession().find(this.cargo);
		this.dmCargo = new ListDataModel(lista);	
		RequestContext.getCurrentInstance().update("formConsultarCargo:dataTableCargo");
		return this.dmCargo;
	}
	
	//INVOCAR TELAS
	public String newCargo() {
		this.cargo = new Cargo();
		return "manterCargo";
	}
	
	public String consultarCargo() {
		this.cargo = new Cargo();
		return "consultarCargo";
	}
	

	public String editarCargo() {
		this.cargo = (Cargo) this.dmCargo.getRowData();
		return "editarCargo";
	}
	
	public String excluirCargo() {
		this.cargo = (Cargo) this.dmCargo.getRowData();
		return "excluirCargo";
	}
	
	
	public void clean() {
		this.cargo = new Cargo();
	}
	

	private int currentTab = 0;

	public int getCurrentTab() {
	    return currentTab;
	}

	public void setCurrentTab(int currentTab) {
	    this.currentTab = currentTab;
	}
	
    public void onTabChange(TabChangeEvent event) {  
        //FacesMessage msg = new FacesMessage("Tab Changed", "Active Tab: " + event.getTab().getTitle());  
        System.out.println(event.getTab().getTitle());
        if(event.getTab().getTitle() == "Cargo") {
        	this.setCurrentTab(0);
        	RequestContext.getCurrentInstance().update("tabViewCargo");
        } else if (event.getTab().getTitle() == "Atividade") {
        	this.setCurrentTab(1);
        	RequestContext.getCurrentInstance().update("tabViewCargo");
        } else if (event.getTab().getTitle() == "Milestone") {
        	this.setCurrentTab(2);    	
        	RequestContext.getCurrentInstance().update("tabViewCargo");
        	//RequestContext.getCurrentInstance().update("formManterMilestone");
        }
        
        //FacesContext.getCurrentInstance().addMessage(null, msg);  
        System.out.println("CurrentTab: " + this.currentTab);       

    }


    
    
	
}
